package com.xiaoqi.reggie.Interceptor;
import com.alibaba.fastjson.JSON;
import com.xiaoqi.reggie.common.BaseContext;
import com.xiaoqi.reggie.common.R;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：xiaoqi
 * @creat 2022/9/18 15:59
 */
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //检查是否登陆
        Object employee = request.getSession().getAttribute("employee");
        Object user = request.getSession().getAttribute("user");
        if (employee!=null){
            //将用户id放进线程中
            BaseContext.setCurrentId((Long) employee);
            return true;
        }
        if (user!=null){
            //将用户id放进线程中
            BaseContext.setCurrentId((Long) user);
            return true;
        }else if (user==null&&employee==null){
            //说明没登陆
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return false;
        }
        return false;
    }
}
